<?php
/**
 * 打赏红包排序和详情的管理
 *
 * Author: lyun
 * Date: 2018/7/18
 * Created by Panxsoft.
 */


namespace App\Http\Controllers\App\WebView;

use App\Http\Controllers\ApiController;
use App\Http\Requests\App\BonusRankingIndexRequest;
use App\Models\Admin\StoreAdmin;
use App\Models\OpenWechat\WechatUser;
use App\Models\Panx\SkinRecordBonus;
use App\Models\Store\Store;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;

class BonusRankingController extends ApiController
{
    const ROUTES = [
        'index' => 'api.web.view.index',
        'show'  => 'api.web.view.show',
        'query' => 'api.web.view.query',
    ];

    /**
     *  App显示 返回店铺店员领用红包的排序
     * @test
     *
     * @param BonusRankingIndexRequest $request
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function index(BonusRankingIndexRequest $request)
    {
        /** @var Store $store */
        $store = Store::find($request->store_id);
            $this->checkItemExist($store, trans('admin.store'));

        /** @var Collection $store_admins */
        $store_admins =  $store->storeAdmins()
            ->filterHasSkinRecordBonusesGranted()
            ->with('skinRecordBonuses', 'wechatUser')
            ->get();

        $bonus_rankings = $this->transformBonusRankings($store_admins);

        return $this->response(compact('bonus_rankings'));
    }

    /**
     * App显示 店员领用红包的记录详情
     * @test
     *
     * @param $store_admin_id
     *
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function show($store_admin_id)
    {
        $store_admin = StoreAdmin::find($store_admin_id);
            $this->checkItemExist($store_admin, trans('admin.store_admin'));

        $bonuses = $store_admin->skinRecordBonuses()
            ->where('bonus_status', SkinRecordBonus::BONUS_STATUS_SUCCESS)
            ->latest()
            ->get();

        $headimgurl = optional($store_admin->wechatUser)->headimgurl;

        $total_amount = number_format($bonuses->sum('amount'), 2);

        return $this->response(compact('bonuses', 'headimgurl', 'total_amount'));
    }

    /**
     * @deprecated - App 不清理 - 已放到微信端！
     *
     * 微信用户领用红包的记录详情
     * @test
     *
     * @param $openid
     *
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function queryWechatUserBonuses($openid)
    {
        /** @var WechatUser $wechat_user */
        $wechat_user = WechatUser::where('openid', $openid)
            ->first();
        $this->checkItemExist($wechat_user, trans('app.wechat_user_error'));

        $bonuses = $wechat_user->skinRecordBonuses()
            ->latest()
            ->get();

        $headimgurl = $wechat_user->headimgurl;

        $total_amount = number_format($bonuses->sum('amount'), 2);

        return $this->response(compact('bonuses', 'headimgurl', 'total_amount'));
    }

    /**
     * @param Collection $store_admins
     * @return Collection|\Illuminate\Support\Collection
     */
    private function transformBonusRankings(Collection $store_admins)
    {
        return $store_admins->map(function (StoreAdmin $store_admin) {
            $store_admin->num = $store_admin->skinRecordBonuses->count();
            $store_admin->headimgurl = optional($store_admin->wechatUser)->headimgurl;

            $sum = $store_admin->skinRecordBonuses->sum('amount');
            $store_admin->total_amount = number_format($sum, 2);

            $store_admin->addHidden(['skinRecordBonuses', 'wechatUser']);
            return $store_admin;
        })
            ->sortByDesc(function($store_admin){
                return $store_admin->total_amount;
            })
            ->values();
    }
}
